package gwt.immunology.client.components;

import static gwt.immunology.shared.GenericDtoKeys.BIRTHDAY;
import static gwt.immunology.shared.GenericDtoKeys.CITY;
import static gwt.immunology.shared.GenericDtoKeys.COUNTRY;
import static gwt.immunology.shared.GenericDtoKeys.CREATION_DATE;
import static gwt.immunology.shared.GenericDtoKeys.DISTRICT;
import static gwt.immunology.shared.GenericDtoKeys.FIRST_NAME;
import static gwt.immunology.shared.GenericDtoKeys.FLAT;
import static gwt.immunology.shared.GenericDtoKeys.GENDER;
import static gwt.immunology.shared.GenericDtoKeys.HOUSE;
import static gwt.immunology.shared.GenericDtoKeys.ID;
import static gwt.immunology.shared.GenericDtoKeys.LAST_NAME;
import static gwt.immunology.shared.GenericDtoKeys.MIDDLE_NAME;
import static gwt.immunology.shared.GenericDtoKeys.OTHER_INFO;
import static gwt.immunology.shared.GenericDtoKeys.REGION;
import static gwt.immunology.shared.GenericDtoKeys.STREET;

import java.util.Date;

import gwt.immunology.client.i18n.ImmunologyI18N;
import gwt.immunology.client.view.AllPatientsView.Presenter;
import gwt.immunology.shared.GenericDto;
import gwt.immunology.shared.enums.GenderEnum;
import gwt.immunology.shared.utils.ImmunologyUtils;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ViewEditPatientBox extends DialogBoxEx {

    private GenericDto patient;
    private Presenter presenter;
    private ImmunologyI18N i18n = GWT.create(ImmunologyI18N.class);
    private TextBox lastName;
    private TextBox firstName;
    private TextBox middleName;
    private DateBoxEx birthday;
    private TextBox country;
    private TextBox region;
    private TextBox district;
    private TextBox cityVillage;
    private TextBox street;
    private TextBox house;
    private TextBox flat;
    private DateBoxEx cardCreationDate;
    private ListBox genderListBox;
    private TextArea cardAdditionalInfoTextArea;
    
    private static final String CELL_WIDTH_160PX = "160px";
    private static final String ROW_HEIGHT = "30px";
    private static final String LABEL_WIDTH = "120px";
    private static final String TEXTBOX_WIDTH = "130px";
    private static final String ASTERISK = "*";
    private static final String EMPTY = "";
    
    public ViewEditPatientBox(GenericDto patient, Presenter presenter) {
        this("&nbsp;", patient, presenter);
    }

    public ViewEditPatientBox(String captionTitle, final GenericDto patientDto, final Presenter presenter) {
        super(captionTitle);
        
        this.patient = patientDto;
        this.presenter = presenter;
        
        VerticalPanel container = new VerticalPanel();
        
        HorizontalPanel dataContainer = new HorizontalPanel();
        dataContainer.getElement().getStyle().setMargin(10, Unit.PX);
        
        VerticalPanel patientInfo = new VerticalPanel();
        patientInfo.setWidth("300px");
        
        //Creating lastNamePanel-------------------------------------------------------------------
        HorizontalPanel lastNamePanel = new HorizontalPanel();
        lastNamePanel.setSize("100%", ROW_HEIGHT);
        lastNamePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label lastNameLabel = new Label("lastName:");
        lastNameLabel.setWidth(LABEL_WIDTH);
        lastNameLabel.addStyleName("boldText");
        
        lastName = new TextBox();
        lastName.setText(this.patient.getString(LAST_NAME));
        lastName.setWidth(TEXTBOX_WIDTH);
        
        lastNamePanel.add(lastNameLabel);
        lastNamePanel.add(lastName);
        lastNamePanel.add(createAsteriskOrEmptyLabel(ASTERISK));
        lastNamePanel.setCellHorizontalAlignment(lastNameLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        lastNamePanel.setCellHorizontalAlignment(lastName, HasHorizontalAlignment.ALIGN_RIGHT);
        lastNamePanel.setCellWidth(lastName, CELL_WIDTH_160PX);
        patientInfo.add(lastNamePanel);
        //-----------------------------------------------------------------------------------------
        
        //Creating firstNamePanel-------------------------------------------------------------------
        HorizontalPanel firstNamePanel = new HorizontalPanel();
        firstNamePanel.setSize("100%", ROW_HEIGHT);
        firstNamePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label firstNameLabel = new Label("firstName:");
        firstNameLabel.setWidth(LABEL_WIDTH);
        firstNameLabel.addStyleName("boldText");
        
        firstName = new TextBox();
        firstName.setText(this.patient.getString(FIRST_NAME));
        firstName.setWidth(TEXTBOX_WIDTH);
        
        firstNamePanel.add(firstNameLabel);
        firstNamePanel.add(firstName);
        firstNamePanel.add(createAsteriskOrEmptyLabel(ASTERISK));
        firstNamePanel.setCellHorizontalAlignment(firstNameLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        firstNamePanel.setCellHorizontalAlignment(firstName, HasHorizontalAlignment.ALIGN_RIGHT);
        firstNamePanel.setCellWidth(firstName, CELL_WIDTH_160PX);
        patientInfo.add(firstNamePanel);
        //-----------------------------------------------------------------------------------------

        //Creating middleNamePanel-------------------------------------------------------------------
        HorizontalPanel middleNamePanel = new HorizontalPanel();
        middleNamePanel.setSize("100%", ROW_HEIGHT);
        middleNamePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label middleNameLabel = new Label("middleName:");
        middleNameLabel.setWidth(LABEL_WIDTH);
        middleNameLabel.addStyleName("boldText");
        
        middleName = new TextBox();
        middleName.setText(this.patient.getString(MIDDLE_NAME));
        middleName.setWidth(TEXTBOX_WIDTH);
        
        middleNamePanel.add(middleNameLabel);
        middleNamePanel.add(middleName);
        middleNamePanel.add(createAsteriskOrEmptyLabel(ASTERISK));
        middleNamePanel.setCellHorizontalAlignment(middleNameLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        middleNamePanel.setCellHorizontalAlignment(middleName, HasHorizontalAlignment.ALIGN_RIGHT);
        middleNamePanel.setCellWidth(middleName, CELL_WIDTH_160PX);
        patientInfo.add(middleNamePanel);
        //-----------------------------------------------------------------------------------------
        
        //Creating genderPanel-------------------------------------------------------------------
        HorizontalPanel genderPanel = new HorizontalPanel();
        genderPanel.setSize("100%", ROW_HEIGHT);
        genderPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label genderLabel = new Label("gender:");
        genderLabel.setWidth(LABEL_WIDTH);
        genderLabel.addStyleName("boldText");
        
        genderListBox = new ListBox();
        genderListBox.setWidth("135px");
        genderListBox.addItem("Male", GenderEnum.MALE.toString());
        genderListBox.addItem("Female", GenderEnum.FEMALE.toString());
        String gender = this.patient.getString(GENDER);
        genderListBox.setItemSelected(0, GenderEnum.valueOf(gender).equals(GenderEnum.MALE));
        genderListBox.setItemSelected(1, GenderEnum.valueOf(gender).equals(GenderEnum.FEMALE));
        genderListBox.setVisibleItemCount(1);
        
        genderPanel.add(genderLabel);
        genderPanel.add(genderListBox);
        genderPanel.add(createAsteriskOrEmptyLabel(ASTERISK));
        genderPanel.setCellHorizontalAlignment(genderLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        genderPanel.setCellHorizontalAlignment(genderListBox, HasHorizontalAlignment.ALIGN_RIGHT);
        genderPanel.setCellWidth(genderListBox, CELL_WIDTH_160PX);
        patientInfo.add(genderPanel);
        //-----------------------------------------------------------------------------------------
        
        //Creating birthdayPanel-------------------------------------------------------------------
        HorizontalPanel birthdayPanel = new HorizontalPanel();
        birthdayPanel.setSize("100%", ROW_HEIGHT);
        birthdayPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label birthdayLabel = new Label("birthday:");
        birthdayLabel.setWidth(LABEL_WIDTH);
        birthdayLabel.addStyleName("boldText");
        
        birthday = new DateBoxEx(new DatePickerEx());
        birthday.setWidth(TEXTBOX_WIDTH);
        birthday.setValue(this.patient.getDate(BIRTHDAY));
        
        birthdayPanel.add(birthdayLabel);
        birthdayPanel.add(birthday);
        birthdayPanel.add(createAsteriskOrEmptyLabel(ASTERISK));
        birthdayPanel.setCellHorizontalAlignment(birthdayLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        birthdayPanel.setCellHorizontalAlignment(birthday, HasHorizontalAlignment.ALIGN_RIGHT);
        birthdayPanel.setCellWidth(birthday, CELL_WIDTH_160PX);
        patientInfo.add(birthdayPanel);
        //-----------------------------------------------------------------------------------------
        
        //Creating countryPanel-------------------------------------------------------------------
        HorizontalPanel countryPanel = new HorizontalPanel();
        countryPanel.setSize("100%", ROW_HEIGHT);
        countryPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label countryLabel = new Label("country:");
        countryLabel.setWidth(LABEL_WIDTH);
        countryLabel.addStyleName("boldText");
        
        country = new TextBox();
        country.setText(this.patient.getString(COUNTRY));
        country.setWidth(TEXTBOX_WIDTH);
        
        countryPanel.add(countryLabel);
        countryPanel.add(country);
        countryPanel.add(createAsteriskOrEmptyLabel(ASTERISK));
        countryPanel.setCellHorizontalAlignment(countryLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        countryPanel.setCellHorizontalAlignment(country, HasHorizontalAlignment.ALIGN_RIGHT);
        countryPanel.setCellWidth(country, CELL_WIDTH_160PX);
        patientInfo.add(countryPanel);
        //-----------------------------------------------------------------------------------------
        
        //Creating regionPanel-------------------------------------------------------------------
        HorizontalPanel regionPanel = new HorizontalPanel();
        regionPanel.setSize("100%", ROW_HEIGHT);
        regionPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label regionLabel = new Label("region:");
        regionLabel.setWidth(LABEL_WIDTH);
        regionLabel.addStyleName("boldText");
        
        region = new TextBox();
        region.setText(this.patient.getString(REGION));
        region.setWidth(TEXTBOX_WIDTH);
        
        regionPanel.add(regionLabel);
        regionPanel.add(region);
        regionPanel.add(createAsteriskOrEmptyLabel(EMPTY));
        regionPanel.setCellHorizontalAlignment(regionLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        regionPanel.setCellHorizontalAlignment(region, HasHorizontalAlignment.ALIGN_RIGHT);
        regionPanel.setCellWidth(region, CELL_WIDTH_160PX);
        patientInfo.add(regionPanel);
        //-----------------------------------------------------------------------------------------
        
        //Creating districtPanel-------------------------------------------------------------------
        HorizontalPanel districtPanel = new HorizontalPanel();
        districtPanel.setSize("100%", ROW_HEIGHT);
        districtPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label districtLabel = new Label("district:");
        districtLabel.setWidth(LABEL_WIDTH);
        districtLabel.addStyleName("boldText");
        
        district = new TextBox();
        district.setText(this.patient.getString(DISTRICT));
        district.setWidth(TEXTBOX_WIDTH);
        
        districtPanel.add(districtLabel);
        districtPanel.add(district);
        districtPanel.add(createAsteriskOrEmptyLabel(EMPTY));
        districtPanel.setCellHorizontalAlignment(districtLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        districtPanel.setCellHorizontalAlignment(district, HasHorizontalAlignment.ALIGN_RIGHT);
        districtPanel.setCellWidth(district, CELL_WIDTH_160PX);
        patientInfo.add(districtPanel);
        //-----------------------------------------------------------------------------------------
        
        //Creating cityVillagePanel-------------------------------------------------------------------
        HorizontalPanel cityVillagePanel = new HorizontalPanel();
        cityVillagePanel.setSize("100%", ROW_HEIGHT);
        cityVillagePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label cityVillageLabel = new Label("City / Village:");
        cityVillageLabel.setWidth(LABEL_WIDTH);
        cityVillageLabel.addStyleName("boldText");
        
        cityVillage = new TextBox();
        cityVillage.setText(this.patient.getString(CITY));
        cityVillage.setWidth(TEXTBOX_WIDTH);
        
        cityVillagePanel.add(cityVillageLabel);
        cityVillagePanel.add(cityVillage);
        cityVillagePanel.add(createAsteriskOrEmptyLabel(ASTERISK));
        cityVillagePanel.setCellHorizontalAlignment(cityVillageLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        cityVillagePanel.setCellHorizontalAlignment(cityVillage, HasHorizontalAlignment.ALIGN_RIGHT);
        cityVillagePanel.setCellWidth(cityVillage, CELL_WIDTH_160PX);
        patientInfo.add(cityVillagePanel);
        //-----------------------------------------------------------------------------------------
        
        //Creating streetPanel-------------------------------------------------------------------
        HorizontalPanel streetPanel = new HorizontalPanel();
        streetPanel.setSize("100%", ROW_HEIGHT);
        streetPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label streetLabel = new Label("street:");
        streetLabel.setWidth(LABEL_WIDTH);
        streetLabel.addStyleName("boldText");
        
        street = new TextBox();
        street.setText(this.patient.getString(STREET));
        street.setWidth(TEXTBOX_WIDTH);
        
        streetPanel.add(streetLabel);
        streetPanel.add(street);
        streetPanel.add(createAsteriskOrEmptyLabel(ASTERISK));
        streetPanel.setCellHorizontalAlignment(streetLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        streetPanel.setCellHorizontalAlignment(street, HasHorizontalAlignment.ALIGN_RIGHT);
        streetPanel.setCellWidth(street, CELL_WIDTH_160PX);
        patientInfo.add(streetPanel);
        //-----------------------------------------------------------------------------------------
        
        //Creating housePanel-------------------------------------------------------------------
        HorizontalPanel housePanel = new HorizontalPanel();
        housePanel.setSize("100%", ROW_HEIGHT);
        housePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label houseLabel = new Label("house:");
        houseLabel.setWidth(LABEL_WIDTH);
        houseLabel.addStyleName("boldText");
        
        house = new TextBox();
        house.setText(this.patient.getString(HOUSE));
        house.setWidth(TEXTBOX_WIDTH);
        
        housePanel.add(houseLabel);
        housePanel.add(house);
        housePanel.add(createAsteriskOrEmptyLabel(ASTERISK));
        housePanel.setCellHorizontalAlignment(houseLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        housePanel.setCellHorizontalAlignment(house, HasHorizontalAlignment.ALIGN_RIGHT);
        housePanel.setCellWidth(house, CELL_WIDTH_160PX);
        patientInfo.add(housePanel);
        //-----------------------------------------------------------------------------------------
        
        //Creating flatPanel-------------------------------------------------------------------
        HorizontalPanel flatPanel = new HorizontalPanel();
        flatPanel.setSize("100%", ROW_HEIGHT);
        flatPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label flatLabel = new Label("flat:");
        flatLabel.setWidth(LABEL_WIDTH);
        flatLabel.addStyleName("boldText");
        
        flat = new TextBox();
        flat.setText(this.patient.getString(FLAT));
        flat.setWidth(TEXTBOX_WIDTH);
        
        flatPanel.add(flatLabel);
        flatPanel.add(flat);
        flatPanel.add(createAsteriskOrEmptyLabel(EMPTY));
        flatPanel.setCellHorizontalAlignment(flatLabel, HasHorizontalAlignment.ALIGN_RIGHT);
        flatPanel.setCellHorizontalAlignment(flat, HasHorizontalAlignment.ALIGN_RIGHT);
        flatPanel.setCellWidth(flat, CELL_WIDTH_160PX);
        patientInfo.add(flatPanel);
        //-----------------------------------------------------------------------------------------

        //creating separatorPanel-------------------------------------------------------------------
        HorizontalPanel separatorPanel = new HorizontalPanel();
        separatorPanel.setSize("30px", "100%");
        //-----------------------------------------------------------------------------------------
        
        VerticalPanel medicineCardInfo = new VerticalPanel();
        medicineCardInfo.setSize("300px", "100%");
        
        //Creating cardPanel-------------------------------------------------------------------
        HorizontalPanel cardCreationDatePanel = new HorizontalPanel();
        cardCreationDatePanel.setSize("100%", ROW_HEIGHT);
        cardCreationDatePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        
        Label cardCreationLabel = new Label("creation date:");
        cardCreationLabel.setWidth(LABEL_WIDTH);
        cardCreationLabel.addStyleName("boldText");
        
        cardCreationDate = new DateBoxEx(new DatePickerEx());
        cardCreationDate.setWidth(TEXTBOX_WIDTH);
        cardCreationDate.setValue(this.patient.getDate(CREATION_DATE));
        
        cardCreationDatePanel.add(cardCreationLabel);
        cardCreationDatePanel.add(cardCreationDate);
        cardCreationDatePanel.add(createAsteriskOrEmptyLabel(ASTERISK));
        cardCreationDatePanel.setCellHorizontalAlignment(cardCreationDate, HasHorizontalAlignment.ALIGN_RIGHT);
        cardCreationDatePanel.setCellWidth(cardCreationDate, CELL_WIDTH_160PX);
        medicineCardInfo.add(cardCreationDatePanel);
        
        Label cardAdditionalInfoLabel = new Label("additional info:");
        cardAdditionalInfoLabel.setWidth(LABEL_WIDTH);
        cardAdditionalInfoLabel.addStyleName("boldText");
        medicineCardInfo.add(cardAdditionalInfoLabel);
        medicineCardInfo.setCellHeight(cardAdditionalInfoLabel, ROW_HEIGHT);
        medicineCardInfo.setCellVerticalAlignment(cardAdditionalInfoLabel, HasVerticalAlignment.ALIGN_MIDDLE);
        
        cardAdditionalInfoTextArea = new TextArea();
        cardAdditionalInfoTextArea.setText(this.patient.getString(OTHER_INFO));
        cardAdditionalInfoTextArea.setSize("300px", "290px");
        
        medicineCardInfo.add(cardAdditionalInfoTextArea);
        medicineCardInfo.setCellHeight(cardAdditionalInfoTextArea, "100%");
        //-----------------------------------------------------------------------------------------

        dataContainer.add(patientInfo);
        dataContainer.add(separatorPanel);
        dataContainer.add(medicineCardInfo);
        dataContainer.setCellWidth(patientInfo, "100%");
        dataContainer.setCellWidth(medicineCardInfo, "100%");
        dataContainer.setCellHeight(medicineCardInfo, "350px");
        
        Button saveButton = new Button("save");
        saveButton.addStyleName("button");
        Button cancelButton = new Button("cancel");
        cancelButton.addStyleName("button");
        Button deleteButton = new Button("delete");
        deleteButton.addStyleName("button");
        
        saveButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                if (isRequiredFieldsEntered()) {
                    GenericDto patient = new GenericDto();
                    patient.put(ID, patientDto.getLong(ID));
                    patient.put(LAST_NAME, lastName.getText());
                    patient.put(FIRST_NAME, firstName.getText());
                    patient.put(MIDDLE_NAME, middleName.getText());
                    patient.put(GENDER, genderListBox.getValue(genderListBox.getSelectedIndex()));
                    patient.put(BIRTHDAY, birthday.getValue());
                    patient.put(COUNTRY, country.getText());
                    patient.put(CITY, cityVillage.getText());
                    patient.put(STREET, street.getText());
                    patient.put(HOUSE, house.getText());
                    patient.put(CREATION_DATE, cardCreationDate.getValue());
                    if (!region.getText().trim().isEmpty()) {
                        patient.put(REGION, region.getText().trim());
                    }

                    if (!district.getText().trim().isEmpty()) {
                        patient.put(DISTRICT, district.getText().trim());
                    }
                    
                    if (!flat.getText().trim().isEmpty()) {
                        patient.put(FLAT, new Integer(flat.getText().trim()));
                    }
                    
                    if (!cardAdditionalInfoTextArea.getText().trim().isEmpty()) {
                        patient.put(OTHER_INFO, cardAdditionalInfoTextArea.getText().trim());
                    }
                    
                    presenter.updatePatientInfo(patient);
                }
            }
        });
        
        cancelButton.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                hide();
            }
        });
        
        deleteButton.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                presenter.deletePatient(patientDto.getLong(ID));
            }
        });
        
        HorizontalPanel buttonsPanel = new HorizontalPanel();
        buttonsPanel.addStyleName("buttonsPanel");
        buttonsPanel.addStyleName("saveDeleteButtonsPanel");
        
        HorizontalPanel saveCancelPanel = new HorizontalPanel();
        saveCancelPanel.addStyleName("margin_10px");
        saveCancelPanel.add(saveButton);
        saveCancelPanel.add(cancelButton);
        
        HorizontalPanel deletePanel = new HorizontalPanel();
        deletePanel.addStyleName("margin_10px");
        deletePanel.add(deleteButton);
        
        buttonsPanel.add(saveCancelPanel);
        buttonsPanel.add(deletePanel);
        buttonsPanel.setCellWidth(saveCancelPanel, "95%");
        buttonsPanel.setCellWidth(deletePanel, "5%");
        buttonsPanel.setCellHorizontalAlignment(saveCancelPanel, HasHorizontalAlignment.ALIGN_CENTER);
        buttonsPanel.setCellHorizontalAlignment(deletePanel, HasHorizontalAlignment.ALIGN_RIGHT);
        
        container.add(dataContainer);
        container.add(buttonsPanel);
        add(container);
    }

    private Label createAsteriskOrEmptyLabel(String text) {
        Label asterisk = new Label(text);
        asterisk.setWidth("10px");
        asterisk.addStyleName("requiredField");
        return asterisk;
    }
    
    private boolean isRequiredFieldsEntered() {
        boolean result = true;

        if (!isDigitTextBoxCorrect(this.flat)) {
            Window.alert(i18n.digitalFieldRequiredMessage());
            result = false;
        }

        if (this.lastName.getText().trim().isEmpty() || 
            this.firstName.getText().trim().isEmpty() || 
            this.middleName.getText().trim().isEmpty() || 
            (this.birthday.getValue() == null) || 
            this.country.getText().trim().isEmpty() || 
            this.cityVillage.getText().trim().isEmpty() || 
            this.street.getText().trim().isEmpty() || 
            this.house.getText().trim().isEmpty() ||
            this.cardCreationDate.getValue() == null) {
                Window.alert(i18n.requiredFieldsMessageAsterisks());
                result = false;
        }

        return result;
    }
    
    private boolean isDigitTextBoxCorrect(final TextBox textBox) {
        String text = textBox.getText();
        if (!text.isEmpty() && !ImmunologyUtils.isDigit(text)) {
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                @Override
                public void execute() {
                    textBox.setFocus(true);
                    textBox.setSelectionRange(0, textBox.getText().length());
                }
            });
            return false;
        }
        return true;
    }
}
